﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.ZLog;

namespace ConApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            InitLog();

            //Console.WriteLine("Dm");
            SQLServerTest.Test(11);
            DmTest.Test(22);
            SQLiteTest.Test(33);
            PostgreSQLTest.Test(44);
            MysqlTest.Test(55);
            //OracleTest.Test(66);//oracl安装后没能跑起来
            //Console.WriteLine("========================================================");

            Console.WriteLine("q exit");
            Console.ReadKey();
        }



        private static void InitLog()
        {
            ZLoger.OutputOption = ZLogerOutputOptions.Loger;
            var redirectAppenderToUI = (RedirectAppender)Loger.GetAppenderByName(null, "RedirectToUI");
            if (redirectAppenderToUI != null)
            {
                redirectAppenderToUI.RedirectOuput += RedirectLogOutput;
            }
        }

        private static void RedirectLogOutput(object sender, RedirectOuputArgs e)
        {
            ConsoleColor fontColor;
            string str;
            try
            {
                var item = e.Item;
                str = string.Format("{0} {1}", e.Item.Time.ToString(LogConstant.DateTimeFormat), e.Item.Content);

                switch (item.Level)
                {
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Trace:
                        fontColor = ConsoleColor.Gray;
                        break;
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Debug:
                        fontColor = ConsoleColor.Gray;
                        break;
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Info:
                        fontColor = ConsoleColor.White;
                        break;
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Warn:
                        fontColor = ConsoleColor.Yellow;
                        break;
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Error:
                        fontColor = ConsoleColor.Red;
                        break;
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Fatal:
                        fontColor = ConsoleColor.Red;
                        break;
                    default:
                        return;
                }
            }
            catch (Exception ex)
            {
                fontColor = ConsoleColor.Red;
                str = ex.Message;
            }

            var oldForegroundColor = Console.ForegroundColor;
            Console.ForegroundColor = fontColor;
            Console.WriteLine(str);
            Console.ForegroundColor = oldForegroundColor;
        }
    }
}
